#ifndef __DEVICE_DEF_H__
#define __DEVICE_DEF_H__

#include "plat/nonecopyable.h"

/// @brief 方向定义
enum class DeviceDirection
{
    None = 0,
    CW,
    CCW
};

template <typename TDevice, typename TId, int MAXSIZE> 
class ICanMgrDevice : public NoneCopyable
{
public:
    static inline TDevice *GetDeviceById(TId deviceId)
    {
        if(!CheckDeviceId(deviceId))
            return nullptr;

        return m_devices[(int)deviceId];
    }

    static inline void ForeachAll(const std::function<void(TDevice*)> &&func)
    {
        for(int i = 0; i < MAXSIZE; i++)
        {
            if(m_devices[i] != nullptr)
            {
                func(m_devices[i]);
            }
        }
    }

    static inline void AddDevice(TId deviceId, TDevice *device)
    {
        if(device == nullptr)
            return;
            
        if(!CheckDeviceId(deviceId))
            return;

        m_devices[(int)deviceId] = device;
    }

    static inline void RemoveDevice(TId deviceId)
    {
        if(!CheckDeviceId(deviceId))
            return;

        m_devices[(int)deviceId] = nullptr;
    }

private:
    static inline bool CheckDeviceId(TId deviceId)
    {
        return (int)deviceId >= 0 || (int)deviceId < MAXSIZE;
    }

private:
    static TDevice *m_devices[MAXSIZE];
};

template <typename TDevice, typename TId, int MAXSIZE> 
TDevice* ICanMgrDevice<TDevice, TId, MAXSIZE>::m_devices[];

#endif // __DEVICE_DEF_H__
